const express = require('express')
const router = express.Router()
const {
    queryShopTotal,
    queryUserTotal,
    queryGoodComments,
    queryReturnBackUsers,
    queryUnloadShop,
    querySaleTotal,
    figureForSales,
    figureForClassify,
    figureForUserBuy
} = require('../../model/mongo')

router.get('/user/sales', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取销量总数'
    }
    result.count = await querySaleTotal()
    res.send(JSON.stringify(result))
})

router.get('/user/total', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取用户总数'
    }
    result.count = await queryUserTotal()
    res.send(JSON.stringify(result))
})

router.get('/pro/total', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取商品总数'
    }
    result.count = await queryShopTotal()
    res.send(JSON.stringify(result))
})

router.get('/pro/lack', async (req, res) => {
    const result = {
        code: 200,
        msg: '商品缺货/下架'
    }
    result.count = await queryUnloadShop()
    res.send(JSON.stringify(result))
})

router.get('/comment/good', async (req, res) => {
    const result = {
        code: 200,
        msg: '好评数'
    }
    result.count = await queryGoodComments()
    res.send(JSON.stringify(result))
})

router.get('/user/back', async (req, res) => {
    const result = {
        code: 200,
        msg: '回头客'
    }
    result.count = await queryReturnBackUsers()
    res.send(JSON.stringify(result))
})

router.get('/sales', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取销量统计图',
        data: []
    }
    result.data = await figureForSales()
    res.send(JSON.stringify(result))
})

router.get('/classify', async (req, res) => {
    const result = {
        code: 200,
        msg: '获取分类销量统计图',
        data: []
    }
    result.data = await figureForClassify()
    res.send(JSON.stringify(result))
})

router.get('/user/buy', async (req, res) => {
    const { userid } = req.query
    const result = {
        code: 200,
        msg: '获取用户购买情况'
    }
    result.data = await figureForUserBuy(userid)
    res.send(JSON.stringify(result))
})

module.exports = router
